package cn.itsource.blog.controller;

import cn.itsource.blog.domain.Param;
import cn.itsource.blog.query.ParamQuery;
import cn.itsource.blog.service.IParamService;
import cn.itsource.until.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 参数接口
 * */
@RestController
@RequestMapping("/param")
@Api(tags = "参数接口类") // 用于描述整个API接口的信息，包括API的标题、描述等
public class ParamController {
    @Autowired
    private IParamService paramService;

    /**
     * 查询所有
     * */
    @GetMapping
    @ApiOperation(value = "查询所有") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public List<Param> getAll(){
        return paramService.getAll();
    }

    /**
     * 查询单个
     * */
    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public Param getOne(@PathVariable("id")Long id){
        return paramService.getOne(id);
    }

    /**
     * 删除单个
     * */
    @ApiOperation(value = "删除单个")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            paramService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Param param){
        try {
            if (param.getId() == null) {
                paramService.add(param);
            }else {
                paramService.update(param);}
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
            paramService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * */
    @ApiOperation(value = "分页查询+高级查询")
    @PostMapping
    public PageInfo<Param> pageQuery(@RequestBody ParamQuery query){
        return paramService.pageQuery(query);
    }

    /**
     * 获取博客参数
     * */
    @GetMapping("/getParams")
    @ApiOperation(value = "获取博客参数") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public Map<String,Object> getParams(){
        return paramService.getParams();
    }

}
